from fuzzyfinder import fuzzyfinder
import tkinter as tk
import pyperclip


class Example(tk.Tk):
    def __init__(self, suggest):
        super().__init__()
        self.iconphoto(False, tk.PhotoImage(file='title.png'))
        self.title('模糊查询')
        self.wm_attributes('-topmost', 1)

        sw = self.winfo_screenwidth()
        sh = self.winfo_screenheight()

        ww = 1000
        wh = 850
        self.geometry("%dx%d+%d+%d" % (ww, wh, (sw - ww) / 2, (sh - wh) / 2))
        self.after(1, lambda: self.focus_force())
        menubar = tk.Menu(self)

        self.suggest = suggest
        # valid percent substitutions (from the Tk entry man page)
        # %d = Type of action (1=insert, 0=delete, -1 for others)
        # %i = index of char string to be inserted/deleted, or -1
        # %P = value of the entry if the edit is allowed
        # %s = value of entry prior to editing
        # %S = the text string being inserted or deleted, if any
        # %v = the type of validation that is currently set
        # %V = the type of validation that triggered the callback
        #      (key, focusin, focusout, forced)
        # %W = the tk name of the widget

        vcmd = (self.register(self.onValidate), '%d', '%i', '%P', '%s', '%S',
                '%v', '%V', '%W')
        self.entry = tk.Entry(self, validate="key", validatecommand=vcmd)
        self.entry.focus_set()
        self.entry.pack(side="top", fill="x")
        bg_frame = tk.Frame(self)
        bg_frame.pack()

        croll_bar = tk.Scrollbar(bg_frame, orient=tk.VERTICAL)
        croll_bar.pack(side=tk.RIGHT, fill=tk.Y)
        croll_bar_x = tk.Scrollbar(bg_frame, orient=tk.HORIZONTAL)
        croll_bar_x.pack(side=tk.BOTTOM, fill=tk.X)
        self.db_list = tk.StringVar()
        self.list_box = tk.Listbox(bg_frame,
                                   listvariable=self.db_list,
                                   bd=1,
                                   relief='groove',
                                   font=('微软雅黑', 8),
                                   selectbackground='brown',
                                   width=160,
                                   height=100,
                                   yscrollcommand=croll_bar.set,
                                   xscrollcommand=croll_bar_x.set)
        self.list_box.bind('<<ListboxSelect>>', self.show_info)
        self.list_box.pack(side=tk.RIGHT)
        croll_bar.config(command=self.list_box.yview)
        croll_bar_x.config(command=self.list_box.xview)
        self.text = tk.Text(self, )
        self.text.pack()

    def onValidate(self, d, i, P, s, S, v, V, W):
        # self.text.insert("end","OnValidate:\n")
        # self.text.insert("end","d='%s'\n" % d)
        # self.text.insert("end","i='%s'\n" % i)
        # self.text.insert("end","P='%s'\n" % P)
        # self.text.insert("end","s='%s'\n" % s)
        # self.text.insert("end","S='%s'\n" % S)
        # self.text.insert("end","v='%s'\n" % v)
        # self.text.insert("end","V='%s'\n" % V)
        # self.text.insert("end","W='%s'\n" % W)

        # Disallow anything but lowercase letters
        # valid = (S.lower() == S)
        # if not valid:
        #     self.bell()
        # return valid

        self.show_s = fuzzyfinder(P, self.suggest)

        self.db_list.set(tuple(self.show_s))
        return 1

    def show_info(self, event):
        try:
            pyperclip.copy(
                self.list_box.get(
                    self.list_box.curselection()[0]).split('keywords')[0])
        except IndexError as e:
            pass


if __name__ == "__main__":
    with open('.\\myjson\\fuzzy_tools.txt', 'r', encoding='utf-8') as f:
        content = f.read()

    root = Example(content.split('\n'))

    root.mainloop()
